package org.ltc.alliance.service.fav;

import org.ltc.alliance.constant.ErrorConst;
import org.ltc.alliance.constant.StatusConst;
import org.ltc.alliance.dao.FavDao;
import org.ltc.alliance.exception.BusinessException;
import org.ltc.alliance.model.FavDomain;
import org.ltc.alliance.service.club.ClubService;
import org.ltc.alliance.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class FavServiceImpl implements FavService{

    @Autowired
    FavDao favDao;
    @Autowired
    UserService userService;
    @Autowired
    ClubService clubService;

    @Override
    public void saveFavInfo(FavDomain fav) {

        // 检查社团是否存在且可以正常招收成员
        clubService.clubStatusNormalByCid(fav.getCid());
        // 检查用户是否存在
        userService.userIdExist(fav.getUid());

        if (Objects.isNull(fav.getFstatus())) {
            throw new BusinessException(ErrorConst.Fav.FAV_RESULT_LOST);
        }

        FavDomain favExist = favDao.getFavByUidAndCid(fav.getUid(), fav.getCid());
        if (null != favExist) {
            if (favExist.getFstatus().equals(fav.getFstatus())) {
                throw new BusinessException(ErrorConst.Fav.CHECK_OUT_MULTIPLE);
            }
            favDao.alterFavStatus(fav);
        }
        else {
            favDao.saveFavInfo(fav);
        }


    }

    @Override
    public List<FavDomain> getFavListByUid(Integer uid, StatusConst.Fav status) {
        userService.userStatusNormalByUid(uid);
        return favDao.getListByUid(uid, status);
    }
}
